import 'package:flutter/material.dart';

class AppLifePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _AppLifePageState();
  }
}

class _AppLifePageState extends State<AppLifePage> with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    super.dispose();
    WidgetsBinding.instance.removeObserver(this);
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    print('状态 改变 $state');
    switch (state) {
      case AppLifecycleState.detached:
        print('清理 资源 ');
        break;
      case AppLifecycleState.resumed:
        print('应用在前台 恢复动画  网络 ');

        break;
        throw UnimplementedError();
      case AppLifecycleState.inactive:
        print('应用不活跃 失去焦点 ');
        break;
      case AppLifecycleState.hidden:
        print('应用完全隐藏 停止不必要的操作');
        break;
      case AppLifecycleState.paused:
        print('应用进入 后台 停止不必要的操作');
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('生命周期页面')),
      body: SizedBox.expand(
        child: Container(
          color: Colors.purple[100],
          child: Center(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                OutlinedButton(
                  onPressed: () {
                    a = 4000;
                    setState(() {});
                  },
                  child: Text('刷新'),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  int a = 2000;
}
